<template>
  <div id="main">
    <el-container>
      <el-header class="main-header">
        <a href="http://www.bestproperty.com.mm" target="_blank">
        <span style='color:#f60; display: inline-block'>
          <img class="logo-header" src="static/logoh.png" alt="">BestProperty<!--<font class="balck">Developer</font>-->
        </span>
        </a>
        <!--<el-menu
          class="el-menu-demo"
          mode="horizontal"
          background-color="#fff"
          text-color="#ffd04b">
          <el-submenu index="1">
            <template slot="title">Select</template>
            <el-menu-item index="1-1" @click.native="signOut">Sign Out</el-menu-item>
          </el-submenu>
        </el-menu>
        <span class="userName">{{this.$store.state.userName || username}}</span>
        </el-menu>-->
        <el-dropdown class="el-menu-demo">
        <span class="el-dropdown-link">{{$t('m.Select')}}<i class="el-icon-arrow-down el-icon--right"></i>
        </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="signOut" style="min-width: 100px;text-align: center;">{{$t('m.Logout')}}</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <span class="changeLang" @click="changeLang">{{lang}}</span>
        <span class="userName">{{this.$store.state.userName || username}}</span>
      </el-header>
      <el-container class="sidebar nav-bar">
        <el-aside class="container" width="260px">
          <el-row>
            <el-col :span="24">
              <el-menu
                default-active="1-1"
                class="el-menu-vertical-demo"
                background-color="#FAFAFA"
                text-color="#999"
                active-text-color="#000"
                :unique-opened="false"
                :default-openeds="defaultopenedsArr"
              >
                <!--background-color="#324157"-->
                <!--text-color="#fff"-->
                <!--active-text-color="#ffd04b"-->

                <!--原先的导航-->
                <!--<el-submenu index="1">-->
                  <!--<template slot="title">-->
                    <!--<i class="el-icon-menu"></i>-->
                    <!--<span>{{$t('m.Advertisement')}}</span>-->
                  <!--</template>-->
                  <!--&lt;!&ndash;<el-menu-item-group>&ndash;&gt;-->
                    <!--&lt;!&ndash;<el-menu-item index="1-1" @click="$router.push('/devlopmentList')">{{$t('m.Developer')}}</el-menu-item>&ndash;&gt;-->
                  <!--&lt;!&ndash;</el-menu-item-group>&ndash;&gt;-->
                  <!--<el-menu-item-group>-->
                    <!--<el-menu-item index="1-1" @click="$router.push('/agentList')">{{$t('m.Agent')}}</el-menu-item>-->
                  <!--</el-menu-item-group>-->
                <!--</el-submenu>-->

                <!--<el-submenu index="2">-->
                  <!--<template slot="title">-->
                    <!--<i class="el-icon-setting"></i>-->
                    <!--<span>{{$t('m.Account')}}</span>-->
                  <!--</template>-->
                  <!--<el-menu-item-group>-->
                    <!--<el-menu-item index="2-1" @click="$router.push('/conpany')">{{$t('m.MyProfile')}}</el-menu-item>-->
                    <!--<el-menu-item index="2-3" @click="$router.push('/changePhone')">{{$t('m.LoginMethods')}}</el-menu-item>-->
                  <!--</el-menu-item-group>-->
                <!--</el-submenu>-->

                <!--不要下拉的导航-->
                <!--20181228&#45;&#45;将developer入口注释，agent入口名改成 My ads maguangcheng-->
                <!--<el-menu-item index="1" @click="$router.push('/devlopmentList')">-->
                  <!--<i class="el-icon-setting"></i>-->
                  <!--<span slot="title">{{$t('m.Developer')}}</span>-->
                <!--</el-menu-item>-->

                <el-menu-item index="2" @click="$router.push('/agentList')">
                  <i class="el-icon-menu"></i>
                  <span slot="title">{{$t('m.MyAds')}}</span>
                </el-menu-item>

                <el-menu-item index="3" @click="$router.push('/conpany')">
                  <i class="el-icon-tickets"></i>
                  <span slot="title">{{$t('m.MyProfile')}}</span>
                </el-menu-item>

                <el-menu-item index="4" @click="$router.push('/changePhone')">
                  <i class="el-icon-mobile-phone"></i>
                  <span slot="title">{{$t('m.Settings')}}</span>
                </el-menu-item>

              </el-menu>
            </el-col>
          </el-row>
        </el-aside>
        <el-main class="main">
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
  import { signOutUser } from '@/api/api'

  export default {
    name: 'Main',
    data () {
      return {
        isCollapse: true,
        username: '',
        defaultopenedsArr: ['1', '2'],
        lang:'English'
      }
    },
    methods: {
      // message alert
      messageT (type, msg) {
        this.$message({
          showClose: true,
          message: msg,
          duration: 1500,
          type: type
        })
      },
      // sign out
      signOut () {
        signOutUser({}).then((res) => {
          console.log(res)
          if (res.data) {
            var data = res.data
            if (data.errorID === 0 || data.errorID === 100004) {
              localStorage.removeItem('username')
              this.$store.commit('updateUserName', '')
              this.$store.commit('updateDeveloperId', '')
              this.$store.commit('updateAccount', '')

              // 尝试清除谷歌登录
              try {
                var auth2 = window.gapi.auth2.getAuthInstance()
                auth2.signOut()
              } catch (e) {

              }
              // 尝试清除facebook登录
              try {
                FB.logout(function (response) {
                  // Person is now logged out
                })
              } catch (e) {

              }

              this.$router.push('/login')
            } else {
              console.log(data.errorID)
              this.messageT('error', data.message)
            }
          }
        })
          .catch((error) => {
            console.log(error)
            this.messageT('error', 'error,please try again later')
          })
      },
      initPage () {
        let username = localStorage.getItem('username')
        if (username) {
          this.username = username
        }
      },
      // 切换语言
      changeLang () {
        if(this.$i18n.locale == 'MM'){
          this.$i18n.locale = 'US'
          this.lang = 'ျမန္မာ'
          this.$store.commit('updateLang', 'US')
          localStorage.setItem('lang','US')

        }else if(this.$i18n.locale == 'US'){
          this.$i18n.locale = 'MM'
          this.lang='English'
          this.$store.commit('updateLang', 'MM')
          localStorage.setItem('lang','MM')
        }
      }
    },
    created () {
      this.initPage()
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  /*.el-menu-vertical-demo:not(.el-menu--collapse) {*/
    /*width: 200px;*/
    /*min-height: 400px;*/
  /*}*/
  .main-header {
    position: fixed;
    top: 0;
    right: 0;
    left: 0;
    height: 60px;
    padding-right: 0;
    background: #fff;
    border-bottom: 1px solid #e6e6e6;
    z-index: 100;
  }

  .main-header span {
    color: rgba(0, 0, 0, 0.65);
    font-size: 20px;
    padding-left: 10px;
    float: left;
    line-height: 60px;
  }

  .el-menu-demo {
    float: right;
  }

  .main-header .el-submenu .el-menu-item {
    max-width: 120px;
    min-width: 100px;
    width: 100%;
  }

  .main-header .userName {
    float: right;
    font-size: 16px;
    padding-right: 25px;
  }

  .main-header .changeLang {
    float: right;
    font-size: 16px;
    padding-right: 25px;
    cursor: pointer;
    user-select: none;
  }

  .container {
    /*border-top: 2px solid rgb(40, 52, 70);*/
    position: fixed;
    top: 60px;
    left: 0;
    bottom: 0;
    /*background: #324157;*/
    background: #FAFAFA;
    height: 100%;
  }

  .container .el-menu {
    border-right: 0;
  }

  .main {
    margin-left: 310px;
    overflow: auto;
    /*background: #fff;*/
    padding: 62px 0 20px 0;
  }

  .logo-header {
    width: 30px;
    height: 30px;
    vertical-align: middle;
    margin-right: 5px;
    margin-top: -5px;
  }

  .balck {
    /*color: #060606;*/
    margin-left: 8px;
  }

  .main-header span.el-dropdown-link {
    font-size: 14px;
    padding: 0 10px;
  }
</style>
